package com.xinyue.authorization.dao.impl;

import com.xinyue.authorization.bean.RoleGroup;
import com.xinyue.authorization.dao.RoleGroupDao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xinyue.authorization.bean.Role;
import com.xinyue.authorization.bean.Group;

@Repository("RoleGroupDaoImpl")
public class RoleGroupDaoImpl implements RoleGroupDao {
	@Autowired
    private SessionFactory factory;
    
	//默认方法
	public void save(RoleGroup entity) {
		factory.getCurrentSession().save(entity);
	}
	
	public Integer deleteById(int id) {
		Query q = factory.getCurrentSession().createQuery("delete RoleGroup where id=?");
		q.setParameter(0, id);
		return q.executeUpdate();
	}
	
	@SuppressWarnings("unchecked")
	public List<Role> queryRoleByGroupId(int groupId) {
		Query q = factory.getCurrentSession().createQuery("select o from RoleGroup a,Role o where o.roleId=a.roleId and a.groupId=?");
		q.setParameter(0, groupId);
		return q.list();
	};
	
	@SuppressWarnings("unchecked")
	public List<Group> queryGroupByRoleId(int roleId) {
		Query q = factory.getCurrentSession().createQuery("select o from RoleGroup a,Group o where o.groupId=a.groupId and a.roleId=?");
		q.setParameter(0, roleId);
		return q.list();
	};
	
	public Integer deleteByRoleId(int roleId) {
		Query q = factory.getCurrentSession().createQuery("delete RoleGroup where roleId=:roleId");
		q.setParameter(0, roleId);
		return q.executeUpdate();
	};
	
	public Integer deleteByGroupId(int groupId) {
		Query q = factory.getCurrentSession().createQuery("delete RoleGroup where groupId=:groupId");
		q.setParameter(0, groupId);
		return q.executeUpdate();
	};
}